module AboutYou
  module SDK
    module Model
      ###
      # This class represents an initiate order model
      ###
      class InitiateOrder
        # url of the order
        attr_accessor :url
        # user token for the order
        attr_accessor :user_token
        # app token of the order
        attr_accessor :app_token
        # error code of the order
        attr_accessor :error_code
        # error message of the order
        attr_accessor :error_message

        ###
        # Constructor for the AboutYou::SDK::Model::InitiateOrder class
        #
        # * *Args* :
        #   - +url+ -> url of the order
        #   - +user_token+ -> user token for the order
        #   - +app_token+ -> app token of the order
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::InitiateOrder
        ###
        def initialize(url, user_token, app_token)
          self.url        = url
          self.user_token = user_token
          self.app_token  = app_token

          self
        end

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::InitateOrder
        ###
        def self.create_from_json(json_object)
          order = new(
              json_object['url'],
              json_object['user_token'],
              json_object['app_token']
          )

          order.parse_error_result(json_object)

          order
        end

        ###
        # This method is used for parsing the error result
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        ###
        def parse_error_result(json_object)
          self.error_code       = json_object.key?('error_code') ? json_object['error_code'] : 0
          self.error_message    = json_object.key?('error_message') ? json_object['error_message'] : nil
        end
      end
    end
  end
end
